package com.iflytek.autofly.setting.biz.system;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.format.DateFormat;

import com.iflytek.autofly.utils.FlyLog;

import java.util.Calendar;

/**
 * <b>Description: </b>TODO<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015/11/26</td>
 * <td>15:32</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class TimeControl {

    private static final String TAG = "TimeControl";
    private static final String HOURS_12 = "12";
    private static final String HOURS_24 = "24";

    private Context mContext;

    public TimeControl(Context ctx) {
        mContext = ctx;
    }

    public boolean getAutoTime() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                return Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.AUTO_TIME) > 0;
            } else {
                return Settings.System.getInt(mContext.getContentResolver(), Settings.System.AUTO_TIME) > 0;
            }
        } catch (Settings.SettingNotFoundException e) {
            FlyLog.e(TAG, e);
            return false;
        }
    }

    public void setAutoTime(boolean enable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AUTO_TIME,
                    enable ? 1 : 0);
        } else {
            Settings.System.putInt(mContext.getContentResolver(), Settings.System.AUTO_TIME,
                    enable ? 1 : 0);
        }
    }

    public boolean getTimeFormat24() {
        return DateFormat.is24HourFormat(mContext);
    }

    public void setTimeFormat24(boolean enable) {
        Settings.System.putString(mContext.getContentResolver(),
                Settings.System.TIME_12_24,
                enable ? HOURS_24 : HOURS_12);
        timeUpdated();
    }

    private void timeUpdated() {
        Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
        mContext.sendBroadcast(timeChanged);
    }

    public String getDate() {
        return DateFormat.getDateFormat(mContext).format(Calendar.getInstance().getTime());
    }

    public String getTime() {
        return DateFormat.getTimeFormat(mContext).format(Calendar.getInstance().getTime());
    }

    public void setDate(int year, int month, int day) {
        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, day);
        long when = c.getTimeInMillis();

        if (when / 1000 < Integer.MAX_VALUE) {
            SystemClock.setCurrentTimeMillis(when);
        }
    }

    public void setTime(int hourOfDay, int minute) {
        Calendar c = Calendar.getInstance();

        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        long when = c.getTimeInMillis();

        if (when / 1000 < Integer.MAX_VALUE) {
            SystemClock.setCurrentTimeMillis(when);
        }
    }
}
